package cn.helloworld.util;

import java.util.Date;
import java.util.List;

import org.hibernate.Session;

import cn.helloworld.bean.Event;
import cn.helloworld.bean.Person;

/**
 * 用来管理事件的 管理器
 * 
 * @author hesheng
 * 
 */
public class EventManager {

	public static void main(String[] args) {
		EventManager mgr = new EventManager();
		System.out.println("args[0]" + args[0]);
		if (args[0].equals("store")) {
			mgr.createAndStoreEvent("Your Event", new Date());
		} else if (args[0].equals("list")) {
			List events = mgr.listEvents();
			for (int i = 0; i < events.size(); i++) {
				Event theEvent = (Event) events.get(i);
				System.out.println("Event: " + theEvent.getTitle()
						+ "|| Time: " + theEvent.getDate());
			}
		} else if (args[0].equals("addpersontoevent")) {
			Long eventId = mgr.createAndStoreEvent("My Event", new Date());
			Long personId = mgr.createAndStorePerson("Foo", "Bar");
			mgr.addPersonToEvent(personId, eventId);
			System.out.println("Added person " + personId + " to event "
					+ eventId);
		} else if (args[0].equals("addEmailToPerson")) {
			Long personId = mgr.createAndStorePerson("he", "sheng");
			mgr.addEmailToPerson(personId, "112260085@qq.com");
			System.out.println("hesheng email is: 112260085@qq.com");
		}
		HibernateUtil.getSessionFactory().close();
	}

	/**
	 * 1.2.4. 值类型的集合
	 * @param personId
	 * @param emailAddress
	 */
	private void addEmailToPerson(Long personId, String emailAddress) {
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		Person aPerson = (Person) session.load(Person.class, personId);
		// adding to the emailAddress collection might trigger a lazy load of
		// the collection
		aPerson.getEmailAddresses().add(emailAddress);
		session.getTransaction().commit();
	}

	/**
	 * 创建对象
	 * @param firstname
	 * @param lastname
	 * @return
	 */
	private Long createAndStorePerson(String firstname, String lastname) {
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();

		Person thePerson = new Person();
		thePerson.setFirstname(firstname);
		thePerson.setLastname(lastname);
		Long id = (Long) session.save(thePerson);

		session.getTransaction().commit();
		return id;
	}

	private Long createAndStoreEvent(String title, Date theDate) {
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();

		Event theEvent = new Event();
		theEvent.setTitle(title);
		theEvent.setDate(theDate);
		Long id = (Long) session.save(theEvent);

		session.getTransaction().commit();
		return id;
	}

	private List listEvents() {
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		List result = session.createQuery("from Event").list();
		session.getTransaction().commit();
		return result;
	}

	/**
	 * 创建关联关系
	 * @param personId
	 * @param eventId
	 */
	private void addPersonToEvent(Long personId, Long eventId) {
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		Person aPerson = (Person) session.load(Person.class, personId);
		Event anEvent = (Event) session.load(Event.class, eventId);
		aPerson.getEvents().add(anEvent);
		session.getTransaction().commit();
	}

}
